import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2022-10-17
 * Time: 14:46
 */
public class TestDemo {
    public static int binarySearch(int[] array,int key){
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = (left+right)/2;
            if (array[mid] > key){
                right = mid - 1;
            } else if(array[mid] < key){
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array = {1,2,4,7,15,17,21,32,55,88};
        int ret = binarySearch(array,17);
        System.out.println(ret);
    }
    public static int find(int[] arr, int toFind){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    public static void main9(String[] args) {
        int[] arr = {1,2,3,10,5,6};
        System.out.println(find(arr,10));
    }
    public static double avg(int[] array){
        int sum = 0;
        for (int x:array) {
            sum += x;
        }
        return (double)sum/(double)array.length;
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5,6};
        System.out.println(avg(array));
    }
    public static  int maxNum(int[] array){
        if (array == null){
            return -1;
        }
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max < array[i]){
                max = array[i];
            }
        }
        return max;
    }
    public static void main7(String[] args) {
        int[] array = {12,8,1,2,10};
        System.out.println(maxNum(array));
    }
    public static String myToString(int[] array){
        if (array == null){
            return null;
        }
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str = str + array[i];
            if (i != array.length-1){
                str = str + ",";
            }
        }
        str = str + "]";
        return str;
    }
    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println(myToString(array));
    }
    public static int[] transfrom(int[] array){
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = array[i] * 2;
        }
        return ret;
    }
    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int ret2[] = transfrom(array);
        System.out.println(Arrays.toString(ret2));
    }
    public static void func(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = 2*array[i];
            System.out.print(array[i] + " ");
        }
    }
    public static void main3(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        func(array);
    }
    public static int[] transform2(int[] array){
        int[] ret =new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = 2*array[i];
        }
        return ret;
    }
    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
        int ret2[] = transform2(array);
        System.out.println(Arrays.toString(ret2));
    }

    public static void swap(int[] array){
        int tmp = array[0];
        array[0] = array[1];
        array[1] = tmp;
    }
    public static void main2(String[] args) {
        int a = 10;
        int b = 20;
        int[] array = {10, 20};
        System.out.println("交换前："+array[0]+" "+array[1]);
        swap(array);
        System.out.println("交换后："+array[0]+" "+array[1]);
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
//        array = new int[2];
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        for (int x:array
             ) {
            System.out.print(x + " ");
        }
    }
}
